/**
 * Created by pello  on 2016/11/25.
 * 创建wtmapSetposition 用户设置位置指令
 */
app.controller("wtmapGetPositionController", ["$rootScope", "$scope", "wtmapService",
    function($rootScope, $scope, wtmapService){
        //初始化地图指令
        $scope.mapIntialed = function(option){

            var promise = wtmapService.intialMap(option.mapid, false, 0, $rootScope.arcgisConfig.lods); //同步调用，获取承诺接口
            promise.then(function(mapobj){
                $scope.mapcadObj = mapobj;
                $scope.mapcadObj.addDynamicMap($rootScope.arcgisConfig.baseMap);
                $scope.mapcadObj.zoomTobyXY(option.center.x, option.center.y, option.zoom);

            }, function(data){
                //调用承诺接口reject();
            });
        };
        //获取中心点坐标
        //格式：{"type":"point","x":155307.451,"y":139762.144,"spatialReference":{"wkid":102100,"latestWkid":3857}}
        $scope.getCenter = function(){
            return $scope.mapcadObj.appMap.extent.getCenter();
        }
    }]);

app.directive("wtmapGetPosition", function(){
    return {
        restrict : "EA",
        replace : true,
        template : "<div><img id='centerImg' src='/assets/img/location_32.png' style='z-index:1000;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);' />" +
        '<wt-map mapid="{{option.mapid }}" height="option.height" showgdmap="option.showgdmap" map-intialed="mapIntialed(option)"></wt-map>' +
        "</div>",
        scope : {
            option : "=",
            position : "="
        },
        //require:"wtmapSetPosition",//依赖控制器
        controller : "wtmapGetPositionController",
        link : function(scope, element, attrs, controller){
            if(!scope.option){
                //设置默认参数
                scope.option = {
                    mapid : "getPosition",
                    center : {x : 155307.451, y : 139762.144, lng : 0, lat : 0},
                    zoom : 1,
                    height : 600,
                    showgdmap : false
                };
            }
            //监视地图拖动事件
            element.bind("mouseout", function(){
                utilities.safeApply(scope, function(){
                    scope.position = scope.getCenter();
                });
                // console.log(scope.position);
            });
        }

    };
});

